from django.contrib.admin import SimpleListFilter

from nav.models import Role_nav, Nav_bar


class Role_nav_Filter(SimpleListFilter):
    title = '角色导航'
    parameter_name = 'role_nav'

    def lookups(self, request, model_admin):
        role_navs = Role_nav.objects.all()
        if request.user.is_superuser is False:
            role_navs = Role_nav.objects.filter(createUser=request.user).all()
        list=[]
        for o in role_navs:
            list.append((o.id,o.title))
        return list;

    def queryset(self, request, queryset):
        if self.value() is None:
            return queryset
        return queryset.filter(role_nav=self.value())

class Nar_bar_Filter(SimpleListFilter):
    title = '导航条'
    parameter_name = 'nav_bar'

    def lookups(self, request, model_admin):
        role_navs = Role_nav.objects.all()
        if request.user.is_superuser is False:
            role_navs = Role_nav.objects.filter(createUser=request.user).all()
        nar_bars=Nav_bar.objects.filter(role_nav__in=role_navs).all()
        list=[]
        for o in nar_bars:
            list.append((o.id,o.title))
        return list;

    def queryset(self, request, queryset):
        if request.user.is_superuser:
            return queryset
        role_navs = Role_nav.objects.filter(createUser=request.user).all()
        nar_bars = Nav_bar.objects.filter(role_nav__in=role_navs)
        queryset = queryset.filter(nav_bar__in=nar_bars)

        if self.value() is None:
            return queryset
        return queryset.filter(nav_bar__in=self.value())

